/**
 * Created with JetBrains WebStorm. User: rekey Date: 16/9/13 Time: 15:25 To
 * change this template use File | Settings | File Templates.
 */

var cluster = require('cluster');
var os = require('os');
var fs = require('fs');
var path = require('path');

if (cluster.isMaster) {
    // Fork workers.
    var len = Math.min(os.cpus().length, 3);
    for (var i = 0; i < len; i++) {
        cluster.fork();
    }
    cluster.on('death', function(worker) {
        console.log('worker ' + worker.pid + ' died');
        // if died spwan again
        cluster.fork();
    });
    var pidFile = path.resolve(__dirname, '..') + '/log/pid.log';
    fs.writeFile(pidFile, process.pid, function(err) {
        if (err) {
            console.log(err);
        }
    });
    console.log('Master process running with pid ' + process.pid);
} else {
    require('./app.js');
}